﻿using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeMediator : Mediator {

    [Inject]//自动获取到cubeView
    public CubeView cubeView { get; set; }

    [Inject(ContextKeys.CONTEXT_DISPATCHER)]//使其变成全局的dispatcher
    public IEventDispatcher dispatcher { get; set; }

    //当所有的Inject完成后，调用
    public override void OnRegister()
    {
        cubeView.Init();
        dispatcher.AddListener(Demo1MediatorEvent.ScoreChange, OnScoreChange);

        //通过dispatcher 发起分数请求命令
        dispatcher.Dispatch(Demo1CommandEvent.RequestScore);
    }

    //当cubeView销毁后，调用这个
    public override void OnRemove()
    {
        //base.OnRemove();
        dispatcher.RemoveListener(Demo1MediatorEvent.ScoreChange, OnScoreChange);
    }


    public void OnScoreChange(IEvent evt)
    {
        cubeView.UpdateScore((int)evt.data);
    }
}
